<? 
	include('base.php');
	User::protect();
	include_class('general_media');
	include_class('m2');
	$id = ($_GET['id'] > 0 && is_numeric($_GET['id'])) ? $_GET['id'] : 0;
	$gm = new GeneralMedia();
	if ($id != 0) {
		$ma = MediaArea::get($id);
		if (!db::isError($ma)) { 
			$isNotGeneralMedia = !$ma->isGeneralMedia();
		}
	}

	if ($_POST['submit'] && !db::isError($ma)) {
		$displayType = ($_POST['mediaType'] == 'av') ? 'list' : 'grid';		
		if (!$_POST['title']) {
			$title = '(untitled area)';
		} else {
			$title = $_POST['title'];
		}
		$allowed = ($_POST['mediaType'] == 'av') ? MEDIA_EXTENSIONS_ALLOWED_AV : MEDIA_EXTENSIONS_ALLOWED_PHOTO;		

		$res = ($ma) ? $ma->add(1, $title, $_POST['description'], $allowed, $displayType) : MediaArea::add(1, $title, $_POST['description'], $allowed, $displayType);
		if (!db::isError($res)) {
			header('Location: media.php?id=' . $res->getID());
		}
	}
	$editors = array('description');
	$section = 'media';
	$page_title = 'Add Media Area';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="media.php">Media&nbsp;&#62;</a>
<? 
	if (is_object($ma)) {
		$breadcrumb = $ma->getAreaTrail();
		if ($breadcrumb) { 
			foreach($breadcrumb as $maBC) {
				echo('<a href="media.php?id=' . $_GET['id'] . '">' . $maBC->getTitle() . '&nbsp;&#62;&nbsp;</a>');
			}
		}
	}	
	echo("Add Media Area");
?>
</div>

<? if ($isNotGeneralMedia) {

	Error::outputDialog('Return to Media','media.php', 'You may only edit general media here. Any media that has been added to a specific band member, show or release must be edited in its appropriate section.');


} else if (!db::isError($ma)) { 

	
	if (db::isError($res)) { 
		$res->outputList();
	}
	
	?>

	<h1>new entry:</h1>
	<div class="inset">
	<form id="add_new_entry" action="<?=$_SERVER['PHP_SELF']?>?id=<?=$id?>" method="post">
	<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
	<tr>
		<td width="50%"><strong>Name:</strong><br/>
		<input type="text" class="text" name="title" id="title" value="<?=$_POST['title']?>"></td>
		<td width="50%"><strong>Type of Area:</strong><br/>
		<select name="mediaType">
			<option value="photo"<? if ($_POST['mediaType'] == 'photo') { ?> selected<? } ?>>Photo Gallery</option>
			<option value="av"<? if ($_POST['mediaType'] == 'av') { ?> selected<? } ?>>Audio/Video List</option>
		</select>
		</td>
	</tr>
	</table>
	<div class="edit-form">
		<strong>Description</strong><br/><textarea name="description" id="description" value="" class="full"><?=$_POST['description']?></textarea></td>
	</div>
	<div class="edit-form">
		<input type="submit" value="Add Area" name="submit" class="button">
		<input type="submit" value="Return to Previous Area" name="preview" onclick="location.href='media.php?id=<?=$id?>'; return false" class="button"></td>
	</div>
	</div>

<? } else { ?>

		<?=$ma->outputDialog('Return to Media','media.php');?>
		
<? } ?>

<? include('layout/footer.php'); ?>
